package com.rndemo.views;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.aspsine.swipetoloadlayout.SwipeLoadMoreTrigger;
import com.aspsine.swipetoloadlayout.SwipeRefreshTrigger;
import com.aspsine.swipetoloadlayout.SwipeTrigger;
import com.rndemo.R;

/**
 * Created by aspsine on 15/9/10.
 */
public class RefreshView extends FrameLayout implements SwipeTrigger, SwipeRefreshTrigger,SwipeLoadMoreTrigger {
    private ImageView ivRefresh;

    private RingProgressDrawable ringProgressDrawable;

    public RefreshView(Context context) {
        this(context, null);
    }

    public RefreshView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RefreshView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        ringProgressDrawable = new RingProgressDrawable(context);
        ringProgressDrawable.setColors(getResources().getColor(R.color.theme_color));

        ivRefresh = new ImageView(context);
        ivRefresh.setImageDrawable(ringProgressDrawable);
        LayoutParams lp = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        lp.gravity = Gravity.CENTER;
        ivRefresh.setLayoutParams(lp);

        addView(ivRefresh);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int height = MeasureSpec.getSize(heightMeasureSpec)-getPaddingTop()-getPaddingBottom();
        ivRefresh.getLayoutParams().width = height;
        ivRefresh.getLayoutParams().height = height;
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    public void onRefresh() {
        ringProgressDrawable.start();
    }
    @Override
    public void onLoadMore() {
        ringProgressDrawable.start();
    }
    @Override
    public void onPrepare() {

    }

    @Override
    public void onMove(int y, boolean isComplete, boolean automatic) {
        if (!isComplete) {
            ringProgressDrawable.setPercent((float) (Math.abs(y) /(getHeight()*2.0)));
        }
    }

    @Override
    public void onRelease() {

    }

    @Override
    public void onComplete() {
        ringProgressDrawable.stop();
    }

    @Override
    public void onReset() {
    }

}
